package initialize

import (
	_ "gin-wave-im/docs"
	"gin-wave-im/global"
	"gin-wave-im/middleware"
	"gin-wave-im/router"
	"github.com/gin-gonic/gin"
	ginSwagger "github.com/swaggo/gin-swagger"
	"github.com/swaggo/gin-swagger/swaggerFiles"
	"net/http"
)

func InitRouter() *gin.Engine {
	r := gin.Default()
	fs := global.GnConfig.Local.Path
	r.StaticFS(fs, http.Dir("./"+fs))

	// 跨域
	r.Use(middleware.Cors()) // 如需跨域可以打开
	r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
	global.GnLog.Info("register swagger handler")

	rGroup := r.Group("/api/v1")
	{
		authGroup := rGroup.Group("/auth")
		{
			router.InitAuthRouter(authGroup)
		}

		userGroup := rGroup.Group("/user")
		userGroup.Use(middleware.AuthMiddleware())
		{
			router.InitUserRouter(userGroup)
		}

		attachGroup := rGroup.Group("/attach")
		attachGroup.Use(middleware.AuthMiddleware())
		{
			router.InitAttachRouter(attachGroup)
		}

		contactGroup := rGroup.Group("/contact")
		contactGroup.Use(middleware.AuthMiddleware())
		{
			router.InitContactRouter(contactGroup)
		}

		chatGroup := rGroup.Group("/chat")
		{
			router.InitChatRouter(chatGroup)
		}
		messageGroup := rGroup.Group("/message")
		messageGroup.Use(middleware.AuthMiddleware())
		{
			router.InitMessageRouter(messageGroup)
		}

		communityGroup := rGroup.Group("/community")
		communityGroup.Use(middleware.AuthMiddleware())
		{
			router.InitCommunityRouter(communityGroup)
		}
	}

	return r
}